package ltd.followkba.page;

import android.view.View;

import com.kcbsdk.EventHandler;
import com.kcbsdk.MyWorks.APhotoInfo;

import java.util.ArrayList;
import java.util.List;

import ltd.advskin.MRAM;
import ltd.advskin.MSKIN;
import ltd.advskin.VARIA;
import ltd.advskin.base.BaseList;
import ltd.advskin.base.BasePage;
import ltd.advskin.base.KcListHolder;
import ltd.advskin.base.KcListListener;
import ltd.advskin.base.KcTwoListener;
import ltd.advskin.base.KcTypeListener;
import ltd.advskin.task.LocalPhotoFile;
import ltd.advskin.view.KcHeaderView;
import ltd.advskin.view.KcListView;
import ltd.followkba.R;
import main.MAPI;

import static ltd.advskin.MSKIN.MSTRING;

public class LocalPhotoPage extends BaseList {
    private LocalPhotoFile mLocalPhotoFile;
    private List<APhotoInfo> mAPhotoInfo;

    // 所有的页面都接受同样的方式打开LOG
    public static boolean logEnable;
    public void MLOG(String text) {
        if (logEnable) {
            MSTRING(MAPI.GET_LOG_NAME(this) + text);
        }
    }

    @Override
    public void onInitView() {                       // 使用者需要继承，初始化页面控件等显示
        setLayout(R.layout.page_simple_list);
        mKcHeaderView = (KcHeaderView) findViewById(R.id.khvSimpleList);
        mKcHeaderView.setTitle("本地图片", new KcTypeListener() {
            @Override
            public void onMessage(int type) {
                switch (type) {
                    case KcHeaderView.TYPE_CLICK_LEFT:
                        onFinish();
                        break;
                }
            }
        });
        initPageListView( 1 );
    }
    @Override
    public void onInitData(){                                                                       // onInitView结束，开始加载数据
        mLocalPhotoFile = new LocalPhotoFile();
        mLocalPhotoFile.setListener(new KcTwoListener() {       // 本地图片
            @Override
            public void onMessage(Object object1, Object object2) {
                mAPhotoInfo = (List)object2;
//                if (mAPhotoInfo == null){
//                    mAPhotoInfo = new ArrayList<>();
//                }
//                int index = 0;
//                List<APhotoInfo> lists = (List) object2;
//                int length = (lists != null) ? lists.size() : 0;
//                for (int counter = 0; counter < length; counter++) {
//                    APhotoInfo aPhotoInfo = lists.get(counter);
//                }
//
//                long tick = System.currentTimeMillis();
//                makeListView(index, (List) object2);
//                logListsFileName((List)mAPhotoInfo[index]);        // 测试调试用，查看列表中文件的名字
//                MLOG(String.format("PhotoSelectPage makeListView AA size:%s %s %s", mBPhotoEdit[0]!=null?mBPhotoEdit[0].size():0, mBPhotoEdit[1]!=null?mBPhotoEdit[1].size():0, mBPhotoEdit[2]!=null?mBPhotoEdit[2].size():0));
                MLOG(String.format("PhotoSelectPage makeListView BB %s ", ((List)object2).size()));
                MRAM.mActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showInfo();
                    }
                });
            }
        });
        mLocalPhotoFile.findFile(VARIA.DBS_PHOTO_LOCAL_ALL);
        showInfo();
    }


    @Override
    public void onFinish() {       // 使用者选择性继承，用户按了后退键，不继承就直接关闭了
        setClose();
    }

    private void showInfo() {
//        mKcHeaderView.setTitle(S);
//        本地图片
    }

    private static final int[] item_photo_list = new int[]{
            R.layout.item_photo_list, 2,
            R.id.kbiPhotoList, R.id.kbtDeletePhoto,
    };
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // KcListView的全部处理，放在页面的最后位置统一处理 ///////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    private void initListView(int tab, KcListView kcListView) {
        mKcListView[tab] = kcListView;
        mKcListView[tab].initView(null, item_photo_list, null, new KcListListener() {
            @Override
            public int onMessage(int type, int tab, Object object) {
                return 0;
            }
            @Override
            public int onBindItemHolder(boolean skipImage, int tab, int position, final KcListHolder holder) {
                APhotoInfo aPhotoInfo = (APhotoInfo) getListView(tab, position);
                MSKIN.setBitmap(aPhotoInfo.FileName, holder.mView[0]);
                holder.mView[0].setTag(aPhotoInfo);
                holder.mView[0].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        setHeadOrDelete((APhotoInfo) v.getTag(), false);
                    }
                });
                return 0;
            }
        });
        // (d)列表最后更新日期时间 (n)下次更新日期时间 (c)当前数量 (q)列表最大的数量
        mKcListView[tab].setStatus(false, false, "", "", "");
        mKcListView[tab].setTabNumber(tab);
    }

    private void initPageListView(int tabQty) {
        mKcListView = new KcListView[tabQty];
        if (tabQty == 1) {
            mKcListView[0] = (KcListView) findViewById(R.id.klvSimpleList);
            mKcListView[0].setVisibility(View.VISIBLE);
            initListView(0, mKcListView[0]);
            return;
        }
    }
}

